package com.easou.ps.rpc.temp;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.easou.ps.common.servicehandle.ServiceAddress;
import com.easou.ps.common.servicehandle.ServiceGroupShard;
import com.easou.ps.common.servicehandle.ServicePool;
import com.easou.ps.master.kit.ConfigFactory;
import com.easou.ps.thrift.search.types.WorkerType;

public class ServerAddressFactory {
	private static final Logger LOG = Logger.getLogger("server");
	private static final int WORKER_CONN_NUM = ConfigFactory.getSysConfig().getInt("worker.connection.num", 1);
	private static final ServicePool WORKER_POOL = new ServicePool("Worker", WORKER_CONN_NUM, 0, true);
	
	/* <K,V> -- <id, address> */
	private static final ConcurrentHashMap<Integer, ServiceAddress> ID_ADDRESS_MAP = new ConcurrentHashMap<Integer, ServiceAddress>();
	/* <K,V> -- <address, id> */
	private static final ConcurrentHashMap<ServiceAddress, Integer> ADDRESS_ID_MAP = new ConcurrentHashMap<ServiceAddress, Integer>();
	/* <K,V> -- <id, workertype> */
	private static final ConcurrentHashMap<Integer, WorkerType> ID_WORKERTYPE_MAP = new ConcurrentHashMap<Integer, WorkerType>();
	
	private static final ConcurrentHashMap<String ,String> IP_GROUP_MAP = new ConcurrentHashMap<String ,String>();
	public static void putAddress(String groupName, ServerAddress address) {
		LOG.info("[manage worker] add: " + groupName + " => " + address + " to pool");
		ServiceAddress addr = address.toServiceAddress();
		WORKER_POOL.putAddress(groupName, address.getShardLabel(), addr);
		ID_ADDRESS_MAP.put(address.getId(), addr);
		ADDRESS_ID_MAP.put(addr, address.getId());
		ID_WORKERTYPE_MAP.put(address.getId(), WorkerType.valueOf(groupName));
		IP_GROUP_MAP.put(addr.toString(), address.getShardLabel());
	}
	
	public static void removeAddress(String groupName, ServerAddress address) {
		//TODO remove address
	}
	
	public static List<ServiceAddress> selectAddresses(String groupName) {
		return WORKER_POOL.selectAddresses(groupName);
	}
	
	public static String[] getGroupNames() {
		return WORKER_POOL.getGroupNames();
	}
	
	public static List<ServiceGroupShard> getShards(String groupName) {
		return WORKER_POOL.getGroup(groupName).getShards();
	}
	
	public static ServiceAddress getAddressById(int id) {
		return ID_ADDRESS_MAP.get(id);
	}
	
	public static int getIdByAddress(ServiceAddress address) {
		return ADDRESS_ID_MAP.get(address);
	}
	
	public static WorkerType getWorkerTypeById(int id) {
		return ID_WORKERTYPE_MAP.get(id);
	}
	
	
	public static String getGroupName(String ipAndPort){
		return IP_GROUP_MAP.get(ipAndPort);
	}
	
	public static boolean setOnline(String host, int port, boolean online) {
		if (online) {
			return WORKER_POOL.setOnline(host, port);
		} else {
			return WORKER_POOL.setOffline(host, port);
		}
	}
	
	public static boolean contains(ServerAddress address){
		ServiceAddress addr = new ServiceAddress(address.getIp(), address.getPort());
		return WORKER_POOL.contains(addr);
	}

}
